package com.estronger.shopmanage.utils;

import android.text.TextUtils;

import com.estronger.shopmanage.bean.ReturnMsgEntity;
import com.google.gson.Gson;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;


/**
 * Created by DC-DingYG on 2017/1/19 0019.
 */

public class JsonUtil {
    private JsonUtil() {
    }

    /**
     * 对象转换成json字符串
     *
     * @param obj
     * @return
     */
    public static String toJson(Object obj) {
        Gson gson = new Gson();
        return gson.toJson(obj);
    }

    /**
     * json字符串转成对象
     *
     * @param str
     * @param type
     * @return
     */
    public static <T> T fromJson(String str, Type type) {
        Gson gson = new Gson();
        return gson.fromJson(str, type);
    }

    /**
     * json字符串转成对象
     *
     * @param str
     * @param type
     * @return
     */
    public static <T> T fromJson(String str, Class<T> type) {
        Gson gson = new Gson();
        return gson.fromJson(str, type);
    }

    // 将Json字符串转换成map
    public static Map<String, Object> getJosn(String jsonStr) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        if (!TextUtils.isEmpty(jsonStr)) {
            JSONObject json = new JSONObject(jsonStr);
            Iterator i = json.keys();
            while (i.hasNext()) {
                String key = (String) i.next();
                String value = json.getString(key);
                if (value.indexOf("{") == 0) {
                    map.put(key.trim(), getJosn(value));
                } else if (value.indexOf("[") == 0) {
                    map.put(key.trim(), getList(value));
                } else {
                    map.put(key.trim(), value.trim());
                }
            }
        }
        return map;
    }

    public static List<Map<String, Object>> getList(String jsonStr)
            throws Exception {
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        JSONArray ja = new JSONArray(jsonStr);
        for (int j = 0; j < ja.length(); j++) {
            String jm = ja.get(j) + "";
            if (jm.indexOf("{") == 0) {
                Map<String, Object> m = getJosn(jm);
                list.add(m);
            }
        }
        return list;
    }

    /**
     * @authr DC-ZhuSB
     * 响应头
     **/
    public static ReturnMsgEntity returnMsgEntity(String jsonStr) {
        ReturnMsgEntity rm = new ReturnMsgEntity();
        try {
            JSONObject o = new JSONObject(jsonStr);
            rm.setSuccess(Boolean.valueOf(o.getString("success")));
            rm.setMsgcode(o.getInt("msgcode"));
            rm.setMsg(o.getString("msg"));
            rm.setDataJson(o.getString("data"));
        } catch (Exception e) {
            e.fillInStackTrace();
        }
        return rm;
    }

    /**
     * @authr DC-ZhuSB
     * 响应头
     **/
    public static ReturnMsgEntity getMsgEntity(String jsonStr) throws JSONException{
        ReturnMsgEntity rm = new ReturnMsgEntity();
        JSONObject o = new JSONObject(jsonStr);
        rm.setSuccess(Boolean.valueOf(o.getString("success")));
        rm.setMsgcode(o.getInt("msgcode"));
        rm.setMsg(o.getString("msg"));
        rm.setDataJson(o.getString("data"));
        return rm;
    }

    /**
     * 获取总页数
     */
    public static int getPageCount(int pagesize, int total) {
        //算多少页
        int totalPage = 1;
        if (total > 1 && pagesize > 0) {
            totalPage = total / pagesize;
            if (total % pagesize != 0) {
                totalPage += 1;
            }
        }
        return totalPage;
    }
}
